<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="login-bg-overlay"></div>
        <div class="login-title">
          <h1>医疗健康管理系统</h1>
          <p>为老年人提供全方位的健康管理服务</p>
        </div>
        <div class="login-decoration">
          <div class="decoration-circle"></div>
          <div class="decoration-circle"></div>
          <div class="decoration-circle"></div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-form">
          <div class="form-header">
            <div class="form-logo">
              <el-icon class="logo-icon">
                <Monitor/>
              </el-icon>
            </div>
            <h2>管理员登录</h2>
          </div>
          <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0">
            <el-form-item prop="username">
              <el-input
                  v-model="loginForm.username"
                  placeholder="请输入用户名"
                  prefix-icon="User"
                  size="large"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                  v-model="loginForm.password"
                  type="password"
                  placeholder="请输入密码"
                  prefix-icon="Lock"
                  show-password
                  size="large"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading" size="large">
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive} from 'vue'
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import type {FormInstance, FormRules} from 'element-plus'
import {Monitor} from '@element-plus/icons-vue'
import {login} from '@/api/admin'
import {useUserStore} from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = reactive<FormRules>({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'}
  ]
})

const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        console.log('开始登录...')
        const res = await login(loginForm)
        console.log('登录响应:', res)

        // 这里 res 直接就是 token 字符串（或你在拦截器中返回的任何 data）
        userStore.setToken(res)
        ElMessage.success('登录成功')

        console.log('准备跳转到首页...')
        await router.push('/home')
        console.log('跳转完成')
      } catch (error: any) {
        console.error('登录异常:', error)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-box {
  width: 1200px;
  height: 700px;
  display: flex;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.login-left {
  flex: 1;
  position: relative;
  background: linear-gradient(135deg, #409EFF 0%, #2c3e50 100%);
  overflow: hidden;
}

.login-bg-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.2);
}

.login-title {
  position: relative;
  z-index: 2;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: #fff;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.login-title h1 {
  font-size: 42px;
  margin-bottom: 24px;
}

.login-title p {
  font-size: 20px;
  opacity: 0.9;
}

.login-decoration {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 200px;
  overflow: hidden;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.decoration-circle:nth-child(1) {
  width: 300px;
  height: 300px;
  bottom: -100px;
  right: -100px;
}

.decoration-circle:nth-child(2) {
  width: 200px;
  height: 200px;
  bottom: 50px;
  left: -50px;
}

.decoration-circle:nth-child(3) {
  width: 150px;
  height: 150px;
  bottom: 100px;
  right: 100px;
}

.login-right {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 60px;
  background: linear-gradient(135deg, #ffffff 0%, #f5f7fa 100%);
}

.login-form {
  width: 100%;
  max-width: 450px;
}

.form-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 40px;
}

.form-logo {
  width: 100px;
  height: 100px;
  margin-bottom: 24px;
  border-radius: 50%;
  background: linear-gradient(135deg, #409EFF 0%, #2c3e50 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.logo-icon {
  font-size: 48px;
  color: #fff;
}

.login-form h2 {
  text-align: center;
  color: #333;
  margin-bottom: 48px;
  font-size: 32px;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 18px;
  background: linear-gradient(135deg, #409EFF 0%, #2c3e50 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

:deep(.el-input__wrapper) {
  height: 48px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

:deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
  font-size: 16px;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 12px rgba(64, 158, 255, 0.1);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 2px 12px rgba(64, 158, 255, 0.2);
}
</style> 